package com.manong.bookstore.store.service.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.Query;

import com.manong.bookstore.store.dao.StorekeeperDao;
import com.manong.bookstore.store.entity.StorekeeperEntity;
import com.manong.bookstore.store.service.StorekeeperService;


@Service("storekeeperService")
public class StorekeeperServiceImpl extends ServiceImpl<StorekeeperDao, StorekeeperEntity> implements StorekeeperService {

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<StorekeeperEntity> page = this.page(
//                new Query<StorekeeperEntity>().getPage(params),
//                new QueryWrapper<StorekeeperEntity>()
//        );
//
//        return new PageUtils(page);
//    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1、获取key
        String key = (String) params.get("key");
        QueryWrapper<StorekeeperEntity> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("storekeeper_id",key)
                    .or().like("storekeeper_name",key)
                    .or().like("phone_number",key)
                    .or().like("identity_number",key)
                    .or().like("email",key);
        }

        IPage<StorekeeperEntity> page = this.page(
                new Query<StorekeeperEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public int register(StorekeeperEntity storekeeper) {

        if(baseMapper.selectCount(new QueryWrapper<StorekeeperEntity>().eq("storekeeper_name", storekeeper.getStorekeeperName())) > 0) {
            return -1;
        }

        if(baseMapper.selectCount(new QueryWrapper<StorekeeperEntity>().eq("phone_number", storekeeper.getPhoneNumber())) > 0) {
            return -2;
        }

        baseMapper.insert(storekeeper);
        return 1;
    }


}